/**
 * 
 */
package net.mikoo.seals.agent.biz.services.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;

import net.mikoo.seals.agent.biz.mapper.agent.RoleMapper;
import net.mikoo.seals.agent.biz.mapper.agent.RolePermitMapper;
import net.mikoo.seals.agent.biz.services.RoleServices;
import net.mikoo.seals.agent.common.BaseServices;
import net.mikoo.seals.agent.common.exception.ParametersException;
import net.mikoo.seals.agent.model.Role;
import net.mikoo.seals.agent.model.RolePermit;

/**
 * @author huangxm
 *
 */
@Service
public class RoleServicesImpl extends BaseServices implements RoleServices {

	Logger logger = Logger.getLogger(RoleServicesImpl.class);

	@Autowired
	private RoleMapper roleMapper;


	@Autowired
	private RolePermitMapper rolePermitMapper;

	@Override
	public List<Role> selectByQuery(Role role) {

		List<Role> roles = roleMapper.selectByQuery(role);

		return roles;
	}

	@Override
	public void save(final Role role) {
		bossTransactionTemplate.execute(new TransactionCallbackWithoutResult() {

			@Override
			public void doInTransactionWithoutResult(TransactionStatus status) {
				// 打印执行日志
				try {
					if (role == null) {
						throw new ParametersException("memberLevel is null!");
					}
					if (StringUtils.isBlank(role.getRoleName())) {
						throw new ParametersException("roleName is blank!");
					}

					roleMapper.insert(role);

					// 添加权限
					Long menuIds[] = role.getMenuIds();

					if (menuIds != null && menuIds.length > 0) {
						for (Long menuId : menuIds) {
							RolePermit rolePermit = new RolePermit();
							rolePermit.setRoleId(role.getRoleId());
							rolePermit.setMenuId(menuId);
							rolePermit.setIsEnabled("Y");
							rolePermitMapper.insert(rolePermit);
						}
					}
				} catch (ParametersException e) {
					logger.error("", e);
					status.setRollbackOnly();
					throw e;
				} catch (Exception e) {
					logger.error("", e);
					status.setRollbackOnly();
					throw e;
				}

			}

		});
	}

	

	@Override
	public void modifyByPrimaryKey(Role role) {
		
		bossTransactionTemplate.execute(new TransactionCallbackWithoutResult() {

			@Override
			public void doInTransactionWithoutResult(TransactionStatus status) {
				// 打印执行日志
				try {
					// 打印执行日志
					if (role == null) {
						throw new ParametersException("role is null!");
					}
					
					if(role.getRoleId() == null) {
						throw new ParametersException("RoleId is null!");
					}
					
					if(StringUtils.isBlank(role.getRoleName())) {
						throw new ParametersException("角色名称不能为空");
					}
					
					
					roleMapper.updateByPrimaryKey(role);
					
					rolePermitMapper.deleteByRoleId(role.getRoleId());
					
					//添加权限
					Long menuIds[] = role.getMenuIds();
					
					if(menuIds != null && menuIds.length > 0) {
						for (Long menuId : menuIds) {
							if(menuId != null) {
								RolePermit rolePermit = new RolePermit();
								rolePermit.setRoleId(role.getRoleId());
								rolePermit.setMenuId(menuId);
								rolePermit.setIsEnabled("Y");
								rolePermitMapper.insert(rolePermit);
							}
							
							
						}
					}
				} catch (ParametersException e) {
					logger.error("", e);
					status.setRollbackOnly();
					throw e;
				} catch (Exception e) {
					logger.error("", e);
					status.setRollbackOnly();
					throw e;
				}

			}

		});

	}

	@Override
	public Role loadByPrimaryKey(Long roleId) {
		Role role = roleMapper.selectByPrimaryKey(roleId);
		return role;
	}

	@Override
	public void deleteByPrimaryKey(Long roleId) {

		if (roleId == null) {
			throw new ParametersException("roleId is null!");
		}
		// 如果角色是可用的，同时删除角色权限表的对应数据
		Role roleOld = roleMapper.selectByPrimaryKey(roleId);
		if (roleOld != null && roleOld.getEnabled().equalsIgnoreCase("Y")) {
			rolePermitMapper.deleteByRoleId(roleId);
		}
		roleMapper.deleteByPrimaryKey(roleId);

	}

	@Override
	public List<Role> selectAllEnabled() {

		List<Role> roles = roleMapper.selectAllEnabled();
		return roles;
	}

}
